package com.open.cloud.rbac.config;

import com.alibaba.fastjson.support.spring.FastJsonRedisSerializer;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.StringRedisSerializer;

/**
 * @author wangzengzhou
 * @since 2021/11/16
 */

@Configuration
public class RedisConfig {

    @Bean
    public RedisTemplate<String, Object> redisTemplate(RedisConnectionFactory factory) {
        RedisTemplate<String, Object> template = new RedisTemplate<>();
        template.setConnectionFactory(factory);
        // 使用fastjson序列化
        FastJsonRedisSerializer<Object> fastJsonRedisSerializer = new FastJsonRedisSerializer<>(Object.class);
        // 设置value的序列化规则和 key的序列化规则
        template.setValueSerializer(fastJsonRedisSerializer);
        template.setKeySerializer(new StringRedisSerializer());
        template.setHashKeySerializer(new StringRedisSerializer());
        template.setHashValueSerializer(fastJsonRedisSerializer);
        template.afterPropertiesSet();
        return template;
    }

//    @Bean
//    RedisMessageListenerContainer container(RedisConnectionFactory connectionFactory) {
//        RedisMessageListenerContainer container = new RedisMessageListenerContainer();
//        container.setConnectionFactory(connectionFactory);
//        return container;
//    }
//
////    @Bean
////    public RedisMessageListenerContainer redisMessageListenerContainer() {
////        RedisMessageListenerContainer container = new RedisMessageListenerContainer();
////        container.setConnectionFactory(connectionFactory);
////        //container.addMessageListener(consumeRedis(), topic());
////        container.addMessageListener(listenerAdapter(orderChannelMsg()),new PatternTopic("orderChannel"));
////        container.addMessageListener(listenerAdapter(orderActivitiChannelMsg()),new PatternTopic("orderActivitiChannel"));
////        return container;
////    }
//
//    @Bean
//    public RedisTemplate<String, Object> redisTemplate(RedisConnectionFactory factory) {
//        RedisTemplate<String, Object> template = new RedisTemplate<String, Object>();
//        template.setConnectionFactory(factory);
//        //// key采用String的序列化方式
//        //template.setKeySerializer(new StringRedisSerializer());
//        //// hash的key也采用String的序列化方式
//        //template.setHashKeySerializer(new StringRedisSerializer());
//        //// value序列化方式采用jackson
//        //template.setValueSerializer(new GenericJackson2JsonRedisSerializer());
//        //// hash的value序列化方式采用jackson
//        //template.setHashValueSerializer(new GenericJackson2JsonRedisSerializer());
//        // 使用Jackson2JsonRedisSerialize 替换默认序列化
//        Jackson2JsonRedisSerializer jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer(Object.class);
//        ObjectMapper objectMapper = new ObjectMapper();
//        objectMapper.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);
//        jackson2JsonRedisSerializer.setObjectMapper(objectMapper);
//        // 设置value的序列化规则和 key的序列化规则
//        template.setHashValueSerializer(jackson2JsonRedisSerializer);
//        template.setValueSerializer(jackson2JsonRedisSerializer);
//        // 将redisTemplate的序列化方式更改为StringRedisSerializer
//        template.setHashKeySerializer(new StringRedisSerializer());
//        template.setKeySerializer(new StringRedisSerializer());
//        template.afterPropertiesSet();
//        return template;
//    }
}